package com.cskaoyan.market.aspect;

import com.cskaoyan.market.util.Validation;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.validation.BindingResult;

/**
 * @ClassName ValidAspect
 * @Description: TODO
 * @Author 远志 zhangsong@cskaoyan.onaliyun.com
 * @Date 2023/5/20 10:55
 * @Version V1.0
 **/
@Component
@Aspect
public class ValidAspect {

    @Pointcut("@annotation(com.cskaoyan.market.annotation.ValidParam)")
    public void pt1(){}

    @Before("pt1()")
    public void beforeAdvice(JoinPoint joinPoint){
        //获取的便是请求参数
        Object[] args = joinPoint.getArgs();
        for (Object arg : args) {
            //遍历请求参数。如果是BindingResult类型，那么调用工具类
            if(arg instanceof BindingResult){
                Validation.valid((BindingResult) arg);
            }
        }
    }
}
